import request from '@/utils/request'

// AI聊天接口
export function aiChatStream(data) {
  return request({
    url: '/ai/ai/chat/stream',
    method: 'get',
    params: {
      message: data.message
    },
    responseType: 'text',
    headers: {
      'Accept': 'text/event-stream',
      'Content-Type': 'application/json'
    },
    onDownloadProgress: function(progressEvent) {
      const xhr = progressEvent.target
      const { responseText } = xhr
      // 触发回调，将新的数据片段传递出去
      if (data.onMessage && responseText) {
        const newData = responseText.substring(data.lastLength || 0)
        data.lastLength = responseText.length
        data.onMessage(newData)
      }
    }
  })
}

export default {
  aiChatStream
}

